var app = getApp();
var api = require('../../api.js');
var id
Page({
  data: {
    project: [],
    index: 0,
  },
  shop_change: function(e) {
    var index = e.detail.value;
    this.setData({
      shop_index: index,
    })
  },
  project_change: function(e) {
    var index = e.detail.value;
    this.setData({
      index: index,
    })
  },
  formSubmit: function(e) {
    var that = this
    var info = e.detail.value;
    if (info.num.length == 0) {
      wx.showToast({
        title: '请输入消费人次',
        icon: 'none'
      })
    } else {
      if (that.data.type == 1) {
        if (info.money.length == 0) {
          wx.showToast({
            title: '金额不能为空',
            icon: 'none',
            duration: 2000
          })
          return false
        } else {
          app.ajax(api.check_check, {
            rd_session: wx.getStorageSync('rd_session'),
            type: that.data.type, //核销方式 1 表充值余额核销 2 表次卡核销
            member_id: that.data.id, // 会员id
            fee: info.money,
            project_id: that.data.projectlist[that.data.index].id, // 项目id
            people: info.num, // 消费人次
            card_id: that.data.three == undefined ? "" : that.data.three, //次卡id
          }, (res) => {
            console.log(res)
            if (res.result == "success") {
              wx.showModal({
                title: '提示',
                content: res.message,
                success: function(res) {
                  if (res.confirm) {
                    wx.navigateBack({
                      delta: 1,
                    })
                  }
                }
              })
            }
          });
        }
      } else {
        app.ajax(api.check_check, {
          rd_session: wx.getStorageSync('rd_session'),
          type: that.data.type, //核销方式 1 表充值余额核销 2 表次卡核销
          member_id: that.data.id, // 会员id
          fee: '',
          project_id: that.data.projectlist[that.data.index].id, // 项目id
          people: info.num, // 消费人次
          card_id: that.data.three == undefined ? "" : that.data.three, //次卡id
        }, (res) => {
          console.log(res)
          if (res.result == "success") {
            wx.showModal({
              title: '提示',
              content: res.message,
              success: function(res) {
                if (res.confirm) {
                  wx.navigateBack({
                    delta: 1,
                  })
                }
              }
            })
          }
        });
      }
    }
  },
  onLoad(options) {
    this.setData({
      id: options.id,
      type: options.type,
      three: options.three,
      is_code: true,
      is_card: false,
    })
    // 获取数据
    var that = this
    app.ajax(api.member_info, {
      rd_session: wx.getStorageSync('rd_session'),
    }, (res) => {
      console.log(res)
      that.setData({
        info: res.data.lists
      })
    });
    
    app.ajax(api.shop_list, {
      // rd_session: wx.getStorageSync('rd_session'),
    }, (res) => {
      console.log(res)
      for (var i = 0; i < res.data.project.length; i++) {
        that.data.project.push(res.data.project[i].title)
      }
      that.setData({
        project: that.data.project,
        projectlist: res.data.project
      })
    });
    app.ajax(api.user_cardinfo, {
      member_id: options.id,
    }, (res) => {
      console.log(res, '123132132')
      that.setData({
        cardinfo: res.data.lists
      })
    });

  },




})